<?php defined('SYSPATH') or die('No direct access allowed.');

class Model_Api_Standard_Member
{

    public static function get_grade_list()
    {
        $grade_list = Model_Member::member_grade();
        $result = array();
        foreach ($grade_list as $grade)
        {
            $result[] = array(
                'id' => $grade['id'],
                'name' => $grade['name'],
                'begin_jifen' => $grade['begin'],
                'end_jifen' => $grade['end']
            );
        }
        return $result;

    }

    public static function get_verify_status_list()
    {
        return array(
            array('id' => 0, 'name' => '未认证'),
            array('id' => 1, 'name' => '审核中'),
            array('id' => 2, 'name' => '通过'),
            array('id' => 3, 'name' => '未通过'),
        );

    }

    public static function search($condition)
    {
        $result = array(
            'row_count' => 0,
            'data' => array()
        );

        $where = "
FROM
	sline_member
WHERE
    1=1 ";
        if ($condition['keyword'])
        {
            $where .= " and (
            `nickname` LIKE '%{$condition['keyword']}%'
            or `truename` LIKE '%{$condition['keyword']}%'
            or `email` LIKE '%{$condition['keyword']}%'
            or `mobile` LIKE '%{$condition['keyword']}%'
			  or `cardid` LIKE '%{$condition['keyword']}%'
			  or `address` LIKE '%{$condition['keyword']}%'
			  or `qq` LIKE '%{$condition['keyword']}%'
            )";
        }
        if ($condition['birthday'])
        {
            $where .= " and unix_timestamp(birth_date)=unix_timestamp('{$condition['birthday']}')";
        }
        if ($condition['sex'])
        {
            $where .= " and sex='{$condition['sex']}'";
        }
        if (is_numeric($condition['begin_jifen']))
        {
            $where .= " and jifen>={$condition['begin_jifen']}";
        }
        if (is_numeric($condition['end_jifen']))
        {
            $where .= " and jifen<={$condition['end_jifen']}";
        }
        if ($condition['start_time'])
        {
            $where .= " and jointime>=unix_timestamp('{$condition['start_time']}')";
        }
        if ($condition['end_time'])
        {
            $where .= " and jointime<=unix_timestamp('{$condition['end_time']}')";
        }

        $sql = "select count(*) as row_count {$where}";
        $list = DB::query(Database::SELECT, $sql)->execute()->as_array();
        $result['row_count'] = $list[0]['row_count'];

        $sql = "SELECT
        mid as id,
        nickname,
        pwd,
        truename,
        sex,
        email,
        mobile,
        jifen,
        litpic,
        safequestion,
        safeanswer,
        jointime,
        joinip,
        logintime,
        loginip,
        checkmail,
        checkphone,
        cardid,
        address,
        regtype,
        birth_date,
        constellation,
        qq,
        signature
      {$where}";
        if ($condition['sort'])
        {
            $sql .= " order by {$condition['sort']['property']} {$condition['sort']['direction']}";
        } else
        {
            $sql .= ' order by jointime desc';
        }

        $start = $condition['start'] ? $condition['start'] : 0;
        $limit = $condition['limit'] ? $condition['limit'] : 20;
        $sql .= " LIMIT {$start},{$limit}";


        $list = DB::query(Database::SELECT, $sql)->execute()->as_array();
        if (count($list) <= 0)
            return $result;

        foreach ($list as &$member)
        {
            $member['jointime'] = Model_Api_Standard_System::format_unixtime($member['jointime']);
            $member['logintime'] = Model_Api_Standard_System::format_unixtime($member['logintime']);
            $member['litpic'] = Model_Api_Standard_System::reorganized_resource_url($member['litpic']);
        }

        $result['data'] = $list;
        return $result;

    }

    public static function get_detail($id)
    {
        $sql = "SELECT
            mid as id,
            nickname,
            pwd,
            truename,
            sex,
            email,
            mobile,
            jifen,
            litpic,
            safequestion,
            safeanswer,
            jointime,
            joinip,
            logintime,
            loginip,
            checkmail,
            checkphone,
            cardid,
            address,
            regtype,
            birth_date,
            constellation,
            qq,
            signature
         FROM `sline_member` WHERE mid={$id}";
        $info = DB::query(Database::SELECT, $sql)->execute()->current();
        if (empty($info))
        {
            return null;
        }

        $result = $info;
        $result['jointime'] = Model_Api_Standard_System::format_unixtime($result['jointime']);
        $result['logintime'] = Model_Api_Standard_System::format_unixtime($result['logintime']);
        $result['litpic'] = Model_Api_Standard_System::reorganized_resource_url($result['litpic']);
        $result['litpic'] = empty($result['litpic']) ? Model_Api_Standard_System::reorganized_resource_url(Model_Member::member_nopic()) : $result['litpic'];

        //会员等级信息
        $result['grade_info'] = array();
        $member_grade = DB::select('*')->from('member_grade')->where("{$info['jifen']} between `begin` and `end`")->execute()->current();
        if (!empty($member_grade))
        {
            $result['grade_info'] = array(
                'id' => $member_grade['id'],
                'name' => $member_grade['name'],
                'begin_jifen' => $member_grade['begin'],
                'end_jifen' => $member_grade['end']
            );
        }

        //会员收货地址列表
        $result['address_list'] = array();
        $member_address_list = DB::select('*')->from('member_address')->where("memberid", "=", $info['id'])->execute()->as_array();
        if (count($member_address_list) > 0)
        {
            $result['address_list'] = $member_address_list;
        }

        //会员常用旅客列表
        $result['tourist_list'] = array();
        $tourist_list = DB::select('*')->from('member_linkman')->where("memberid", "=", $info['id'])->execute()->as_array();
        if (count($tourist_list) > 0)
        {
            $result['tourist_list'] = $tourist_list;
        }

        return $result;
    }

    public static function statistics_num($timespan)
    {
        $sql = "SELECT count(*) as num FROM `sline_member` WHERE jointime between {$timespan[0]} and {$timespan[1]}";
        $info = DB::query(Database::SELECT, $sql)->execute()->current();

        return $info['num'];
    }


    public static function save($member)
    {
        $result = array(
            'success' => false,
            'message' => ''
        );

        $id = Arr::get($member, 'mid');

        if ($member['pwd'])
        {
            $member['pwd'] = md5(Arr::get($member, 'pwd'));
        }

        if ($member['email'])
        {
            $flag = Model_Member::checkExist("email", $member['email'], $id);
            if ($flag == 'false')
            {
                $result['message'] = "存在重复的email";
                return $result;
            }
        }

        if ($member['mobile'])
        {
            $flag = Model_Member::checkExist("mobile", $member['mobile'], $id);
            if ($flag == 'false')
            {
                $result['message'] = "存在重复的mobile";
                return $result;
            }
        }

        if ($member['litpic'])
        {
            $litpic = explode(",", $member['litpic']);
            $pic_data = base64_decode($litpic[1]);
            $pic_name = uniqid() . $litpic[0];
            $pic_url = "/uploads/member/{$pic_name}";
            $pic_path = BASEPATH . $pic_url;
            if (!file_exists(dirname($pic_path)))
            {
                if (!mkdir(dirname($pic_path), 0777, true))
                {
                    $result['message'] = "创建头像上传目录失败";
                    return $result;
                }
            }

            if (file_put_contents($pic_path, $pic_data) === false)
            {
                $result['message'] = "头像文件写入失败";
                return $result;
            }

            $member['litpic'] = $pic_url;
        }


        //添加操作
        if (empty($id))
        {
            $member['jointime'] = time();
            $insert_result = DB::insert('member', array_keys($member))->values(array_values($member))->execute();
            $result['success'] = true;
            $result['message'] = $insert_result[0];
        } else
        {
            DB::update('member')->set($member)->where('mid', '=', $id)->execute();
            $result['success'] = true;
            $result['message'] = $id;
        }

        return $result;
    }

    public  static function withdraw_save($member)
    {
        $userid = $member['userid'];
        $amount = doubleval($member['amount']);
        $bankaccountname = $member['bankaccountname'];
        $description = $member['description'];
        $way = 'weixin';
        $bankname = 'xcx';
        $bankcardnumber = 'xcx';
        $member = Model_Member::get_member_info($userid);
        $amount = floor($amount*100)/100;

        $config=Model_Member_Withdraw::get_config();
        $cash_available_num=Model_Member_Withdraw::get_available_num($userid);
        if($config['cash_min']==1&&$amount<$config['cash_min_num'])
        {
            return array('status'=>false,'msg'=>'申请金额不足最低限度');
            exit;
        }
        if ($amount < 0.1) {
            return array('status' => false, 'msg' => '申请金额不能低于0.1');
            exit;
        }
        if($cash_available_num==0)
        {
            return array('status'=>false,'msg'=>'当月可提现次数已用完');
            exit;
        }
        $db = Database::instance();
        $db->begin();
        try
        {
            $curtime = time();
            $useful_money = number_format($member['money']-$member['money_frozen'],2,'.','');
            if($useful_money<$amount)
            {
                throw new Exception('可提现余额不足');
            }
            if($amount<=0)
            {
                throw new Exception('提现金额不得小于0');
            }

            $withdraw_model = ORM::factory('member_withdraw');
            $withdraw_model->memberid = $member['mid'];
            $withdraw_model->withdrawamount = $amount;
            $withdraw_model->bankname = $bankname;
            $withdraw_model->way = $way;
            $withdraw_model->bankcardnumber = $bankcardnumber;
            $withdraw_model->bankaccountname = $bankaccountname;
            $withdraw_model->description = $description;
            $withdraw_model->status = 0;
            $withdraw_model->addtime = $curtime;
            $withdraw_model->save();
            if(!$withdraw_model->saved())
            {
                throw new Exception('提现失败，请重试');
            }

            //会员信息修改
            $member_model = ORM::factory('member',$member['mid']);
            $member_model->money_frozen+=$amount;
            $member_model->save();
            if(!$member_model->saved())
            {
                throw new Exception('提现失败，请重试');
            }

            $log_des = '提交提现申请后冻结'.$amount.'元';
            Model_Member_Cash_Log::add_log($member['mid'],2,$amount,$log_des,array('withdrawid'=>$withdraw_model->id));
            $db->commit();
            return array('status'=>true,'msg'=>'提交申请成功');
            exit;
        }
        catch (Exception $excep)
        {
            $db->rollback();
            return array('status'=>false,'msg'=>$excep->getMessage());
            exit;
        }
    }


    /**
     * @function 统计会员相关模块数量
     * @param $mid
     * @return array
     */
    public static function calculate_num($mid)
    {
        $out = array();

        $member = DB::select()->from('member')->where('mid','=',$mid)->execute()->current();
        // 积分
        $out['jifen'] = empty($member['jifen'])?0:$member['jifen'];
        // 会员等级
        $out['rank'] = Model_Member::member_rank($mid, array('return' => 'current'));
        // 钱包
        $money = $member['money'] ? $member['money'] : 0;
        $money_frozen = $member['money_frozen'] ? $member['money_frozen'] : 0;
        $out['money'] = $money - $money_frozen;

        //优惠券数量
        if(St_Functions::is_normal_app_install('coupon'))
        {

            $out['coupon'] = DB::select('id')->from('member_coupon')->where('mid','=',$mid)->execute()->count();
        }

        //拼团
        if(St_Functions::is_normal_app_install('together'))
        {

            $out['together'] = DB::select('id')->from('together_join_details')
                ->where('memberid','=',$mid)->and_where('status','=',1)
                ->group_by('join_id')->execute()->count();
        }

        //红包
        if(St_Functions::is_normal_app_install('red_envelope'))
        {
            $out['red_envelope'] = DB::select('id')->from('envelope_member')
                ->where('memberid','=',$mid)->and_where('use','=',0)
                ->execute()->count();
        }

        //我的游记数量
        if(St_Functions::is_system_app_install(101))
        {
            $out['notes'] = DB::select('id')->from('notes')->where('memberid','=',$mid)->execute()->count();
        }

        //我的结伴数量
        if(St_Functions::is_system_app_install(11))
        {
            $out['jieban'] = DB::select('id')->from('jieban')->where('memberid','=',$mid)->execute()->count();
        }

        //我的咨询
        $out['question'] = DB::select('id')->from('question')->where('memberid','=',$mid)->execute()->count();

        return $out;
    }



}